#!/bin/bash 

#proot=/c4_working/work_unity1
proot=`accurev info 2>&1| grep Top | awk '{print $2}'`
if [ -z "$proot" ]; then
    proot=`git rev-parse --show-toplevel 2>&1`
    if [ $? != 0 ]; then
        echo "ERROR: not in either accurev or git workspace"
        exit 1
    fi
fi

safesim=$proot/safe/Targets/armada64_checked/simulation/exec
safeuser=$proot/safe/Targets/armada64_checked/user/exec
sade=$proot/sade/obj/KHDATAPATH_DEBUG_64/dart/Dart/server/src/kernel/min_kernel/sade_csx
bm=$proot/sade/obj/KHDATAPATH_DEBUG_64/dart/Dart/server/src/kernel/buffer_mgr/bm_csx
linux=$proot/sade/obj/KHDATAPATH_DEBUG_64/dart/Dart/server/src/sade/sade_core_linux/core/
sm=$proot/sade/obj/KHDATAPATH_DEBUG_64/rpm_install/EMC/Platform/lib64/
stat=$proot/safe/obj/KHDATAPATH_DEBUG_64/rpm_install/opt/observability-producer/lib64/
uutf=$proot/safe/obj/KHDATAPATH_DEBUG_64/rpm_install/opt/c4-uutf-common/lib64/
ApiHelper=$proot/sade/obj/KHDATAPATH_DEBUG_64/rpm_install/opt/safe/lib64/admin/
ldpath=$safesim:$safeuser:$sade:$bm:$linux:$sm:$stat:$uutf:$ApiHelper
ln -sf $proot/safe/catmerge/layered/MLU/test/MUT/ufs64/ufs64sim/ufs64sim.sade.cfg  $proot/

if [ "$1" == "-g" ];then
    debug="-debug"
    jitfile=/tmp/simrun.debug
    tmpfile=$jitfile.tmp
    echo '#!/bin/bash' > $tmpfile
    echo 'gdb "$@"' >> $tmpfile
    chmod a+rwx $tmpfile
    mv $tmpfile $jitfile
    export JIT_DEBUGGER=$jitfile
    shift
fi

exe=$1
shift

param=$@

cmd="$safesim/$exe $param $debug"

cd $proot
echo  "sudo JIT_DEBUGGER=$JIT_DEBUGGER LD_PRELOAD="$MY_LD_PRELOAD" LD_DEBUG=$MY_LD_DEBUG LD_DEBUG_OUTPUT=$MY_LD_DEBUG_OUTPUT LD_LIBRARY_PATH=$ldpath:$LD_LIBRARY_PATH $cmd"
#exit
bash -c "sudo JIT_DEBUGGER=$JIT_DEBUGGER LD_PRELOAD="$MY_LD_PRELOAD" LD_DEBUG=$MY_LD_DEBUG LD_DEBUG_OUTPUT=$MY_LD_DEBUG_OUTPUT LD_LIBRARY_PATH=$ldpath:$LD_LIBRARY_PATH $cmd" 2>&1 | tee run.log

